﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ADDDF.Infrastructure.ChangeTracking.ChangeRecords;

namespace ADDDF.Infrastructure.ChangeTracking.ChangeRecords
{
    class ChangeStore
    {
        private Dictionary<EntityKey, EntityRecord> _entityRecords;
        private Dictionary<IChangeRule, RuleRecord> _ruleRecords;

        public ChangeStore()
        {
            _entityRecords = new Dictionary<EntityKey, EntityRecord>(new EntityKeyEqualityComparer());
            _ruleRecords = new Dictionary<IChangeRule, RuleRecord>(new ChangeRuleEqualityComparer());
        }

        public EntityRecord Find(Type type, object key)
        {
            EntityKey eKey = new EntityKey { Type = type, Key = key };

            if (_entityRecords.ContainsKey(eKey))
                return _entityRecords[eKey];
            else
                return null;
        }

        public IList<EntityRecord> FindAllEntityRecords()
        {
            return _entityRecords.Values.ToList();
        }

        public RuleRecord Find(IChangeRule rule)
        {
            if (_ruleRecords.ContainsKey(rule))
                return _ruleRecords[rule];
            else
                return null;
        }

        public IList<RuleRecord> FindAllRuleRecords()
        {
            return _ruleRecords.Values.ToList();
        }

        public void Add(EntityRecord entityRecord)
        {
            EntityKey eKey = new EntityKey { Type = entityRecord.Type, Key = entityRecord.Key };

            _entityRecords.Add(eKey, entityRecord);
        }

        public void Change(EntityRecord entityRecord)
        {
            EntityKey eKey = new EntityKey { Type = entityRecord.Type, Key = entityRecord.Key };

            _entityRecords[eKey] = entityRecord;
        }

        public void Add(RuleRecord ruleRecord)
        {
            _ruleRecords.Add(ruleRecord.Rule, ruleRecord);
        }
    }
}
